home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / SYS / s / define.wrx < prev    next >
Text File  |  1996-09-26  |  7KB  |  262 lines

  1. /* Easy handling of defines
  2.  *
  3.  * © 1993 by MGR-Software, Asgard
  4.  * written 23/09/93 by Lars Hanke
  5.  * contact: mgr@asgard.bo.open.de
  6.  *
  7.  * Supports:
  8.  *
  9.  *   RENUMBER    -  the #define directive in the current line is evaluated.
  10.  *                  If it doesn't hold a value, 0 is assumed, otherwise it
  11.  *                  is taken as start value. All following #define's will
  12.  *                  be assigned a sequential numbering until a line is
  13.  *                  encountered that does not meet the format: #define NAME
  14.  *
  15.  *   REFERENCE   -  The word under the cursor or left of it is assumed to
  16.  *                  hold a #define'd value. It is first searched in the
  17.  *                  current text. If it cannot be found, a prefix is
  18.  *                  extracted, if the symbol meets the format PREFIX_NAME
  19.  *                  Then the last occurence of PREFIX_ is searched.
  20.  *                  If the search is unsuccessful still, the corresponding
  21.  *                  includefile is openend and the procedure is redone
  22.  *                  there.
  23.  *
  24.  * Installation:
  25.  *
  26.  *   -  Call 'DoRexx "define.wrx" ACTION' from 'WRITE'
  27.  *   -  put '$Includefile = Myfile.h' somewhere in your code (e.g. in
  28.  *      comments), if you want an autolookup in your includes
  29.  *
  30.  */
  31.  
  32. arg action
  33.  
  34. if ~show('P',"WRITE") then
  35. do
  36.   say "This script is useless without the WRITE editor!"
  37.   exit 0
  38. end
  39.  
  40. address 'WRITE'
  41. options results
  42.  
  43. 'VERSIONCHECK 3848 "Define.wrx"'
  44. IF RC~=0 THEN DO
  45.   exit 10
  46. END
  47.  
  48. 'GetVar (_CurrentID)'
  49. ID = RESULT
  50. 'LockWindow' ID
  51.  
  52. select
  53.   when action = "RENUMBER"   then call renumber
  54.   when action = "REFERENCE"  then call reference
  55.   otherwise 'MessageOK (Unbekannte Option:' action ')'
  56. end
  57.  
  58. 'LockWindow 0'
  59. exit 0
  60.  
  61. renumber: procedure
  62.  
  63.   'GetVar (_XPos)'                                   /* get home position */
  64.   a.x = RESULT
  65.   'GetVar (_YPos)'
  66.   a.y = RESULT
  67.  
  68.   'GetVar (_CurrentLine)'                            /* get line and check syntax */
  69.   line = RESULT
  70.   if word(line,1) ~= "#define" then
  71.   do
  72.      'MessageOK (Sorry, this is not a "#define"!)'
  73.      return
  74.   end
  75.   if word(line,2) = "" then
  76.   do
  77.      'MessageOK (Well, a name would be fine!)'
  78.      return
  79.   end
  80.   init = word(line,3)                                /* get start value */
  81.   if init = "" then init = 0
  82.   do until line = ""                                 /* process all valid lines */
  83.      idx = wordindex(line,3)
  84.      if idx = 0 then line = line init                /* create renumbered line */
  85.      else do
  86.         line = delword(line,3)
  87.         line = insert(init,line,idx-1)
  88.      end
  89.      'FirstInLine'
  90.      'DeleteLine'                                    /* kill old line and write new */
  91.      'WriteText "'||line||'"'
  92.      'Return'
  93.      init = init + 1                                 /* increase number */
  94.  
  95.      'GetVar (_CurrentLine)'                         /* check next line for validity */
  96.      line = RESULT
  97.      if word(line,1) ~= "#define" then line = ""
  98.      if word(line,2) = "" then line = ""
  99.   end
  100.  
  101.   'Goto' a.x a.y
  102.  
  103.   return
  104.  
  105. reference: procedure EXPOSE ID OldID ret
  106.  
  107.   'GetVar (_XPos)'                                   /* get home position */
  108.   a.x = RESULT
  109.   'GetVar (_YPos)'
  110.   a.y = RESULT
  111.  
  112.   'GetVar (_CurrentWord)'                            /* get word to reference */
  113.   ref = RESULT
  114.   do while ref = "RESULT"
  115.      CursorLeft 1
  116.      if RC ~= 0 then return
  117.      'GetVar (_CurrentWord)'
  118.      ref = RESULT
  119.   end
  120.  
  121.   p = pos("_",ref)                                   /* get prefix */
  122.   if p ~= 0 then
  123.   do
  124.      pre = left(ref,p)
  125.   end
  126.   else pre = ""
  127.  
  128.   call findref                                       /* seek current file */
  129.   if ret = 0 then return
  130.  
  131.   call getincfile                                    /* get inc, if not found */
  132.  
  133.   if ret ~= 0 then
  134.   do
  135.      'Goto' a.x a.y
  136.      return
  137.   end
  138.  
  139.   call findref                                       /* and seek in includes */
  140.  
  141.   if OldID ~= 0 then
  142.   do
  143.      LockWindow OldID
  144.      'Goto' a.x a.y
  145.      LockWindow ID
  146.   end
  147.  
  148.   return
  149.  
  150. findref: procedure EXPOSE ret ref pre
  151.  
  152.   ret = 0
  153.   'Goto 1 1'
  154.   'SetVar (_FindString) ('||ref||')'
  155.  
  156.   'Find 0'                                     /* find first occurence */
  157.   if RC = 0 then
  158.   do
  159.      'GetVar (_CurrentLine)'
  160.      line = RESULT
  161.      if word(line,1) = "#define" then return   /* #define must be first, so quit */
  162.   end
  163.  
  164.   if pre = "" then                             /* no prefix => not found */
  165.   do
  166.      ret = 10
  167.      return
  168.   end
  169.  
  170.   'Goto 1 1'                                   /* search for prefix */
  171.   'SetVar (_FindString) ('||pre||')'
  172.   f.x = 0
  173.   'Find 0'
  174.   do while RC = 0                              /* until last occurence */
  175.      'GetVar (_CurrentLine)'
  176.      line = RESULT
  177.      if word(line,1) = "#define" then
  178.      do
  179.         'GetVar (_XPos)'                       /* if #define, store positon */
  180.         f.x = RESULT
  181.         'GetVar (_YPos)'
  182.         f.y = RESULT
  183.      end
  184.      'Find 0'
  185.   end
  186.  
  187.   if f.x = 0 then ret=10                       /* goto last valid position */
  188.   else 'Goto' f.x f.y
  189.  
  190.   return
  191.  
  192. getincfile: procedure EXPOSE ID OldID ret
  193.  
  194.   'Goto 1 1'                                   /* find locale tag */
  195.   'SetVar (_FindString) ($Includefile)'
  196.   'Find 0'
  197.   if RC = 0 then
  198.   do
  199.      'GetVar (_CurrentLine)'                    /* parse it */
  200.      line = RESULT
  201.      line = substr(line,pos("$Includefile",line))
  202.      line = delword(line,1,1)
  203.      do i=1 until cnt = 0
  204.         ch = substr(line,i,1)
  205.         if ((ch = ':') | (ch = '=') | (ch = ' ')) then cnt = 1
  206.         else cnt = 0
  207.      end
  208.      line = substr(line,i)
  209.      line = word(line,1)
  210.  
  211.      'GetVar (_CurrentID)'                     /* store ID */
  212.      OldID = RESULT
  213.  
  214.      LockWindow 0                              /* we must of course unlock */
  215.      'NextED 0'                                /* file already loaded ? */
  216.      file = ""
  217.      do while ((RC = 0) & (file ~= line))
  218.        'GetVar (_CurrentID)'
  219.        ID = RESULT
  220.        'GetVar (_File)'
  221.        file = RESULT
  222.        NextED ID
  223.      end
  224.  
  225.      if file = line then
  226.      do
  227.        LockWindow ID                           /* lock and activate window */
  228.        'Goto 1 1'
  229.        'GetVar (_WinMode)'
  230.        if ((RESULT = 1) | (RESULT = 2)) then 'Window 0 0 0 0'
  231.      end
  232.      else do
  233.         LockWindow OldID                       /* if not: get path and open */
  234.         'GetVar (_FILEPATH)'
  235.         path = RESULT
  236.         if right(path,1,':') ~= ':' then path = path || '/'
  237.  
  238.         if ~exists(line) then                  /* check file existance */
  239.         do
  240.            if ~exists(path||line) then
  241.            do
  242.               'MessageOK "Could not open includefile:' line '"'
  243.               ret = 10
  244.               return
  245.            end
  246.            line = path||line
  247.         end
  248.  
  249.         'NewEd ""'                             /* open the tagged file */
  250.         ID = RESULT
  251.         'LockWindow' ID
  252.         'Open "'||line||'"'
  253.         'Window 0 0 0 0'
  254.      end
  255.   end
  256.   else OldID = 0                               /* code: SAME_FILE */
  257.  
  258.   ret = 0
  259.   return
  260.  
  261.  
  262.